﻿using Dysoft.RabbitMQ.Model;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Dysoft.RabbitMQ.Test.Producer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("【生产者】启动成功");

            var message = new MessageQueue();

            //发布广播消息
            PublishRabbitMQMsg(message);
        }

        /// <summary>
        /// 发布消息
        /// </summary>
        static void PublishRabbitMQMsg(MessageQueue message)
        {
            #region RabbitMQ_Demo

            message.Publish("test.channel", "我发出了一条数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            Task.Delay(2000).ContinueWith((t) => message.Publish("test.channel", "延迟发送了一条数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));

            Task.Delay(5000).ContinueWith((t) => message.Publish("test.dynamic.channel",
                new
                {
                    message = "延迟发送了一条匿名类型数据，时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                }));

            #endregion

            #region 广播消息

            Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    var teacherObj = new
                    {
                        msg = $"我发出了一条数据！——【{i}】",
                    };

                    var msgContent = new MQMsgModel()
                    {
                        MessageID = Guid.NewGuid().ToString("N"),
                        MessageTitle = "教师新增",
                        MessageContent = teacherObj,
                        MessageRouter = "Teacher.Insert",
                        MessageSource = "FounPlatform",
                        SendingTime = DateTime.Now      //消息队列时间戳
                    };

                    message.Publish("Teacher.Insert", msgContent);
                    Console.WriteLine($"我发出了一条数据！——【{i}】");

                    Thread.Sleep(1000);
                }
            });

            #endregion

            #region 队列消息

            Task.Run(() =>
            {
                MessageLogDTO log = new MessageLogDTO();
                log.LogID = Guid.NewGuid().ToString("N");
                log.Sender = "";
                log.SendTime = DateTime.Now;
                log.MessageID = Guid.NewGuid().ToString("N");
                log.SendContent = "消息同步日志记录";

                Task.Delay(3000).ContinueWith((t) => message.PublishQueue("dykj_mq_log", log));
            });
            #endregion

            Console.WriteLine("Press 'Enter' to send a message.To exit, Ctrl + C");

            while (Console.ReadLine() != null)
            {
                MessageLogDTO log = new MessageLogDTO();
                log.LogID = Guid.NewGuid().ToString("N");
                log.Sender = "";
                log.SendTime = DateTime.Now;
                log.MessageID = Guid.NewGuid().ToString("N");
                log.SendContent = "消息同步日志记录";

                message.PublishQueue("dykj_mq_log", log);

                Console.WriteLine(" [x] Sent {0}", message);
            }

            Console.WriteLine(" Press [Ctrl + C] to exit.");
            Console.ReadLine();

        }

    }
}
